package com.app.web.goods.goods;

import com.app.utils.base.j2se.Convert;
import com.app.utils.base.j2se.Logger;
import com.app.utils.base.j2se.StringHandler;
import com.app.utils.web.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Description
 * Created by 王伟
 * Date create on 10:39 2019/12/4.
 */
@Component
public class GoodsManager implements Runnable{
    @Autowired
    private GoodsDao goodsDao;

    private static List<Map<String,Object>> goodsPromoteList;
    private static List<Map<String,Object>> goodsPromoteGoods = new ArrayList<>();

    private static List<Map<String,Object>> goodsActList;
    private static Map<Integer,Map<String,Object>> goodsActMap;
    private static List<Map<String,Object>> goodsActGoods = new ArrayList<>();

    @PostConstruct
    void init(){
        new Thread(this).start();
    }

    @Override
    public void run() {
        try {
            while (true) {
                loadGoodsPromote();
                loadGoodsAct();
                Thread.sleep(60000);

            }
        } catch (Exception e) {
            Logger.error("商品管理器加载失败",e);
        }
    }

    /* 加载项目集信息 */
    private void loadGoodsPromote(){
        goodsPromoteList = goodsDao.goodsPromoteList();
        if(StringHandler.isNotEmpty(goodsPromoteList)){
            List<Integer> ids = goodsPromoteList.stream().map(x-> Convert.objToInt(x.get("placeId"))).collect(Collectors.toList());
            goodsPromoteGoods = goodsDao.goodsPromoteGoods(ids);
        }
    }
    /* 根据项目集ID查询项目 */
    public static String goodsByPromote(int id){
        List<Map<String,Object>> promotes = goodsPromoteGoods.stream().filter(x->Convert.objToInt(x.get("placeId"))==id).collect(Collectors.toList());
        return promotes.size()==0?"":Convert.objToStr(promotes.get(0).get("goods_id"));
    }

    /* 加载活动信息 */
    private void loadGoodsAct(){
        goodsActList = goodsDao.goodsActList();
        goodsActMap = goodsActList.stream().filter(x->StringHandler.getInt(x.get("type")) == 2).collect(Collectors.toMap(x-> StringHandler.getInt(x.get("id")),y->y));
        if(StringHandler.isNotEmpty(goodsActList)){
            List<Integer> ids = goodsActList.stream().map(x-> Convert.objToInt(x.get("id"))).collect(Collectors.toList());
            goodsActGoods = goodsDao.goodsActGoods(ids);
        }
    }
    /* 根据活动ID查询项目 */
    public static String goodsByAct(int id){
        List<Map<String,Object>> promotes = goodsActGoods.stream().filter(x->Convert.objToInt(x.get("actId"))==id).collect(Collectors.toList());
        return promotes.size()==0?"":Convert.objToStr(promotes.get(0).get("goods_id"));
    }
    /* 返回活动列表 */
    public static List<Map<String,Object>> getGoodsActList(){
        return goodsActList;
    }

    public Map<String,Object> getGoodsActInfo(int id){
        if(StringHandler.isEmpty(goodsActMap)){
            loadGoodsAct();
        }
        return goodsActMap.containsKey(id)?goodsActMap.get(id):new HashMap<>();
    }

    public static Map<String, Object> toMap(Map<String,Object> goods) {
        String name = Convert.objToStr(goods.get("name"));
        goods.put("shortName",name.length()>18?name.substring(0,18)+"...":name);
        String url = Param.picUrl;
        if(StringHandler.getString(goods.get("banner")).startsWith("http")){
            url = "";
        }
        // 商品图上传路径
        String[] banners = Convert.objToStr(goods.get("banner")).replaceAll(" ","").split(",");
        String finalUrl = url;
        List<String> banner = Arrays.stream(banners).map(String::trim).map(x-> finalUrl +x).collect(Collectors.toList());
        goods.put("banner", banner);
        if(StringHandler.isEmpty(goods.get("cover_pic"))){
            goods.put("cover_pic",banner.get(0));
        }else {
            goods.put("cover_pic",url+Convert.objToStr(goods.get("cover_pic")).trim());
        }
        String[] descPic = Convert.objToStr(goods.get("desc_pic")).split(",");
        List<String> descPics = descPic.length>0?Arrays.stream(descPic).map(String::trim).map(x-> finalUrl + x).collect(Collectors.toList()):new ArrayList<>();
        goods.put("desc_pic", descPics);

        if(StringHandler.isNotEmpty(goods.get("units"))){
            goods.put("units",Convert.objToStr(goods.get("units")).split(","));
        }
        return goods;
    }
}
